package com.mis.geoEncryption.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mis.geoEncryption.model.connect.DatabaseConnector;
import com.mis.geoEncryption.model.data.IItemExtractor;
import com.mis.geoEncryption.model.data.ItemExtractor;
import com.mis.geoEncryption.model.data.ResultGenerator;
import com.mis.geoEncryption.model.xml.IXMLProcessor;
import com.mis.geoEncryption.model.xml.XMLProcessor;

/**
 * Servlet implementation class MainServelet
 */
//@WebServlet("/MainServelet")
public class MainServelet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainServelet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.setContentType("text/xml");
		 
		/**DatabaseConnector db = (DatabaseConnector) getServletContext().getAttribute("db");
		PrintWriter out = response.getWriter();
		String sql = "select * from item";
		ResultSet rs;
		try {
			rs = db.runSql(sql);
 
			while(rs.next()){
				out.println(rs.getString("itemName"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}**/
		/**DatabaseConnector db = (DatabaseConnector) getServletContext().getAttribute("db");
		IItemExtractor itemextractor = new ItemExtractor(db);
		ResultGenerator resultGen = new ResultGenerator(itemextractor);
		String finaleString = resultGen.getXMLString();**/
		/**PrintWriter out = response.getWriter();
		out.println(finaleString);**/
		
		/**OutputStream os = response.getOutputStream();
		OutputStreamWriter osw = new OutputStreamWriter(os);
		PrintWriter pw = new PrintWriter(osw);**/
		//response.getWriter().println(finaleString);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/xml");
		
		String postString = MainServelet.getPostData(request);
		System.out.println("*********************************");
		String vb = postString.replace("null", "");
		System.out.println(vb);
		
		IXMLProcessor processor = new XMLProcessor();
		String finaleString = processor.processXMLMessage(vb);
		response.getWriter().println(finaleString);
		
		/**DatabaseConnector db = (DatabaseConnector) getServletContext().getAttribute("db");
		IItemExtractor itemextractor = new ItemExtractor(db);
		ResultGenerator resultGen = new ResultGenerator(itemextractor);
		String finaleString = resultGen.getXMLString();
		System.out.println(finaleString);
		response.getWriter().println(finaleString);**/
	}
	
	public static String getPostData(HttpServletRequest req) {
	    StringBuilder sb = new StringBuilder();
	    try {
	        BufferedReader reader = req.getReader();
	        reader.mark(10000);

	        String line;
	        do {
	            line = reader.readLine();
	            sb.append(line).append("\n");
	        } while (line != null);
	        reader.reset();
	        // do NOT close the reader here, or you won't be able to get the post data twice
	    } catch(IOException e) {
	       // logger.warn("getPostData couldn't.. get the post data", e);  // This has happened if the request's reader is closed    
	    }

	    return sb.toString();
	}

}
